/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  Implementation of the LocationManager.
*               Location manager queries the current location, saves locations to landmark
                database and sends/receives locations to/from another device.
*
*/

#ifndef LOCATIONMANAGER_H
#define LOCATIONMANAGER_H

/*! \file
 *  \brief Location manager queries the current location, saves locations to landmark
           database and sends/receives locations to/from another device.
 */
#include <QtCore/QObject>
#include <qmobilityglobal.h>
#include <QGeoPositionInfo>

QTM_BEGIN_NAMESPACE
class QGeoPositionInfoSource;
class QLandmarkManager;
class QLandmarkId;
class QLandmarkCategory;
QTM_END_NAMESPACE

QTM_USE_NAMESPACE

/*!
 *  \brief  LocationManager class is used to query and store the current
 *          position. Whenever the cached location changes, the change
            is signalled to UI. LocationManager also constructs/deconstructs
            the shared location data and handles storing of the shared
            locations upon receiving. LocationManager is used from within
            the QML code.
 */
class LocationManager : public QObject
{
    Q_OBJECT

    /*!
     *  \brief Property for latitude. Required by the QML.
     *  \sa latitude(), latitudeChanged()
     */
    Q_PROPERTY( double latitude READ latitude NOTIFY latitudeChanged )

    /*!
     *  \brief Property for longitude. Required by the QML.
     *  \sa longitude(), longitudeChanged()
     */
    Q_PROPERTY( double longitude READ longitude NOTIFY longitudeChanged )

    /*!
     *  \brief Property for location name. Required by the QML.
     *  \sa locationName(), setLocationName(), locationNameChanged()
     */
    Q_PROPERTY( QString locationName READ locationName WRITE setLocationName NOTIFY locationNameChanged )

public:

    //! Constructor.
    /*!
     *  \param parent Parent for the object.
     */
    LocationManager( QObject *parent = 0 );

    //! Destructor.
    /*!
     *  Destructor.
     */
    virtual ~LocationManager();

public Q_SLOTS:

    //! Request a position update.
    /*!
     *  Starts current location request. When position update is available
     *  slot handlePositionUpdate() is called or slot handleUpdateTimeout()
     *  if current position could not be retrieved.
     *
     *  \sa handlePositionUpdate(), handleUpdateTimeout()
     */
    void startLocationUpdate();

    //! Position update request completed successfully.
    /*!
     *  Signalled from the QLandmarkManager instance when the position
     *  update request is successfully completed. Signals positionChanged() and
     *  locationNameChanged() are emitted.
     *
     *  \param update Holds the value of new position.
     */
    void handlePositionUpdate( const QGeoPositionInfo &update );

    //! Position update request failed.
    /*!
     *  Signalled from the QLandmarkManager instance when an updated position
     *  could not be retrieved.
     */
    void handleUpdateTimeout();

    //! Get latitude of the position.
    /*!
     *  Get the latitude of last position update.
     *
     *  \return latitude of the last position.
     */
    double latitude() const;

    //! Get longitude of the position.
    /*!
     *  Get the longitude of last position update.
     *
     *  \return longitude of the last position.
     */
    double longitude() const;

    //! Get name of the location.
    /*!
     *  Get the current position as a string representation. If the current
     *  position has a name, then the name is returned, otherwise a string
     *  representation of the coordinates in decimal degrees format is
     *  returned.
     *
     *  \return String representation of the position.
     */
    QString locationName() const;

    //! Set name of the location.
    /*!
     *  Sets name of the position. A locationNameChanged() signal is
     *  emitted, when the name is set.
     *
     *  \param name Name of the position.
     */
    void setLocationName( const QString &name );

    //! Store current location to landmark database.
    /*!
     *  Stores the current position to landmark database. A signal
     *  locationNameChanged() is emitted, if saving is successful.
     *
     *  \param name Name of the postion.
     */
    void storeCurrentLocation(const QString &name);

    //! Share a location with another device.
    /*!
     *  Start the sharing of the location with another device. Wraps the name and the
     *  position into a byte array. A signal sendLocation() is then emitted.
     *
     *  \param name Name of the position.
     *  \param latitude Position latitude.
     *  \param longitude Position longitude.
     */
    void shareCurrentLocation( const QString &name, double latitude, double longitude );

    //! Receive location from other device.
    /*!
     *  Receive location with provided name and position. Data is read from
     *  byte array \a data and the location is stored to landmark database. Signals
     *  positionChanged() and locationNameChanged() is emitted if location is successfully
     *  saved to landmark database.
     *
     * \param data Byte array holding the name and position data shared from the
     *  other device.
     */
    void receiveLocation( const QByteArray &data );

Q_SIGNALS:

    //! Position changed signal.
    /*!
     *  Signalled when my position is updated.
     *  \param showDialog - true to show dialog to store
     *  location when position is updated.
     *
     *  \sa handlePositionUpdate(), receiveLocation()
     */
    void positionChanged( bool showDialog );

    //! Position latitude changed signal.
    /*!
     *  Signalled when the latitude of the position is updated.
     */
    void latitudeChanged();

    //! Position longitude changed signal.
    /*!
     *  Signalled when my position is updated.
     */
    void longitudeChanged();

    //! Location name changed signal.
    /*!
     *  Signalled when selected location is changed.
     *
     * \sa handlePositionUpdate, setLocationName,
     */
    void locationNameChanged();

    //! Share location.
    /*!
     *  Signalled when location is to be shared with
     *  another device.
     *
     * \sa shareCurrentLocation()
     */
    void sendLocation( const QByteArray &data );

    //! Device connected signal.
    /*!
     *  Signalled when another device has connected to this
     *  device.
     *
     * \param deviceName The name of the newly connected device.
     */
    void deviceConnected( const QString &deviceName );

    //! Shared location received.
    /*!
     *  Signalled when location from other device has been received.
     *
     *  \param locationName Name of the received location.
     */
    void locationReceived( const QString &locationName );

    //! Device disconnected signal.
    /*!
     *  Signalled when other device has disconnected.
     *
     * \param deviceName Name of the disconnected device.
     */
    void deviceDisconnected( const QString &deviceName );

private:
    QGeoPositionInfoSource *m_source;
    QLandmarkManager *m_landmarkManager;
    QLandmarkCategory *m_category;
    QGeoPositionInfo m_current;
    QString m_LocationName;
};

#endif // LOCATIONMANAGER_H

